<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $aro;
	
	function beforeFilter() {
		$user = $this->Auth->user();
		if ($user == null) {
			$this->aro = 'visitors';
		}
		else {
			$this->aro = $this->Acl->Aro->field('alias', array('id' => $this->Auth->user('aro_id')));
		}
		
		parent::beforeFilter();
	}
	
	function _who($id) {
		$who = '';
		if ($id == $this->Auth->user('id')) {
			$who = 'self';
		}
		else {
			$who = 'others';
		}
		return $who;
	}
	
	function login() {
	}
	
	function logout() {
		$this->Auth->logout();
		$this->redirect(array('action' => 'index'));
	}
	
	function index() {
		$accessAllowed = $this->Acl->check($this->aro, 'player_actions/'.$this->params['action']);
		$this->set('accessAllowed', $accessAllowed);
		
		if ($accessAllowed) {
			$this->User->recursive = 0;
			$this->set('users', $this->paginate());
		}
	}

	function view($id = null) {
		$accessAllowed = $this->Acl->check($this->aro, 'player_actions/'.$this->params['action'].'/'.$this->_who($id));
		$this->set('accessAllowed', $accessAllowed);
		
		if ($accessAllowed) {
			if (!$id) {
				$this->Session->setFlash(sprintf(__('Invalid %s', true), 'user'));
				$this->redirect(array('action' => 'index'));
			}
			$this->set('user', $this->User->read(null, $id));
		}
	}

	function add() {
		$accessAllowed = $this->Acl->check($this->aro, 'player_actions/'.$this->params['action']);
		$this->set('accessAllowed', $accessAllowed);
		
		if ($accessAllowed) {
			if (!empty($this->data)) {
				$this->User->create();
				if ($this->User->save($this->data)) {
					$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'user'));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'user'));
				}
			}
			$aros = $this->User->Aro->find('list');
			$this->set(compact('aros'));
		}
	}

	function edit($id = null) {
		$accessAllowed = $this->Acl->check($this->aro, 'player_actions/'.$this->params['action'].'/'.$this->_who($id));
		$this->set('accessAllowed', $accessAllowed);
		
		if ($accessAllowed) {
			if (!$id && empty($this->data)) {
				$this->Session->setFlash(sprintf(__('Invalid %s', true), 'user'));
				$this->redirect(array('action' => 'index'));
			}
			if (!empty($this->data)) {
				if ($this->User->save($this->data)) {
					$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'user'));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'user'));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->User->read(null, $id);
			}
			$aros = $this->User->Aro->find('list');
			$this->set(compact('aros'));
		}
	}

	function delete($id = null) {
		$accessAllowed = $this->Acl->check($this->aro, 'player_actions/'.$this->params['action'].'/'.$this->_who($id));
		$this->set('accessAllowed', $accessAllowed);
		
		if ($accessAllowed) {
			if (!$id) {
				$this->Session->setFlash(sprintf(__('Invalid id for %s', true), 'user'));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->User->delete($id)) {
				$this->Session->setFlash(sprintf(__('%s deleted', true), 'User'));
				$this->redirect(array('action'=>'index'));
			}
		}
		$this->Session->setFlash(sprintf(__('%s #%s was not deleted', true), 'User', $id));
		$this->redirect(array('action' => 'index'));
	}
}
?>